<script setup>
import PageHeader from "@/views/Common/PageHeader.vue";
import {useRouter} from 'vue-router'
import {getGoodsByShopIdAPI} from "@/apis/goods.js";
import {ref,onBeforeMount,computed} from 'vue'
import {getShopByShopIdAPI} from '@/apis/user.js'
import SmallItemCard from "@/components/card/Small-ItemCard.vue";


const router = useRouter()
const shop=ref()
const goodsList=ref([])
const searchName=ref('')

onBeforeMount(()=>{
  getGoods()
  getShopInfo()
})

async function getGoods() {
  const res=await getGoodsByShopIdAPI(router.currentRoute.value.params.id)
  if(res.code===200){
    goodsList.value=res.data
    goodsList.value.forEach(item=>{
      if(typeof item.tags==='string')
      item.tags=item.tags.split(',')
    })
  }
}

async function getShopInfo() {
  const res=await getShopByShopIdAPI(router.currentRoute.value.params.id)
  if(res.code===200){
    shop.value=res.data

  }
}

const goToHome = () => {
  router.push('/homepage')
}

const goodsListFilter=computed(()=>{
  return goodsList.value.filter(item=>{
    const matchesName=searchName.value.length?item.title.includes(searchName.value):true
    return matchesName
  })
})
</script>

<template>
<div class="shopPage">
  <PageHeader></PageHeader>
  <div class="header">
    <img @click="goToHome" style="height: 60px;width: auto;margin-left: 40px;margin-top: 20px" src="@/assets/SAS-logo.png" alt="">
  </div>

  <div class="container">

    <div class="shopTitle">
      <img :src="shop.shopAvatar" style="width: 45px;height: 45px" alt="">
      &nbsp;
      <span class="shopName">{{shop.shopName}}</span>
      &nbsp; &nbsp;
      <div class="shopTags">
        <el-tag type="danger">
          5.0 | <el-icon><StarFilled /></el-icon>
          <el-icon><StarFilled /></el-icon>
          <el-icon><StarFilled /></el-icon>
          <el-icon><StarFilled /></el-icon>
          <el-icon><StarFilled /></el-icon></el-tag>

        <el-tag type="info" style="color: #50607a">平均11小时发货</el-tag>
        <el-tag type="info" style="color: #50607a">客服平均35秒回复</el-tag>
      </div>
      <div class="shopBtn">
        <el-input style="width: 300px;margin-right: 30px" placeholder="店内搜索" v-model="searchName"></el-input>
        <el-button style="color: #121a2f"><el-icon style="color: #008bfd"><Headset /></el-icon>&nbsp;联系客服</el-button>
      </div>
    </div>

    <div class="goods-list">
      <small-item-card v-for="goods in goodsListFilter" :item="goods" @click="router.push(`/detail/${goods.gid}`)"></small-item-card>
    </div>

    <el-empty v-if="goodsListFilter.length===0" description="暂无商品"></el-empty>

  </div>



</div>

</template>

<style scoped lang="less">
.shopPage {
  width: 100%;
  min-height: 100vh;
  background-color: #f8f9fa;
}

.header {
  width: 100%;
  height: 80px;
  background-color: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  display: flex;
  align-items: center;
  padding: 0 40px;
}

.container {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 30px 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.shopTitle {
  width: 100%;
  background-color: white;
  border-radius: 8px;
  padding: 30px;
  margin-bottom: 30px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.06);
  display: flex;
  align-items: center;
  gap: 20px;
  
  img {
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  }
}

.shopName {
  font-size: 24px;
  font-weight: 600;
  color: #11192d;
  font-family: "Microsoft YaHei", serif;
  transition: color 0.3s;
  
  &:hover {
    color: #007bff;
    cursor: pointer;
  }
}

.shopTags {
  display: flex;
  gap: 10px;
  flex: 1;
  
  .el-tag {
    border-radius: 20px;
    padding: 8px 16px;
    font-size: 12px;
    font-weight: 500;
    
    &[type="danger"] {
      background-color: #fff5f5;
      border-color: #ffccc7;
      color: #ff4d4f;
    }
    
    &[type="info"] {
      background-color: #f6f8fa;
      border-color: #d0d7de;
      color: #50607a;
    }
  }
}

.shopBtn {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 20px;
  
  .el-input {
    :deep(.el-input__wrapper) {
      border-radius: 20px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
      
      &.is-focus {
        box-shadow: 0 2px 8px rgba(0, 123, 255, 0.2);
      }
    }
  }
  
  .el-button {
    border-radius: 20px;
    padding: 10px 20px;
    font-weight: 500;
    transition: all 0.3s;
    
    &:hover {
      transform: translateY(-1px);
      box-shadow: 0 4px 12px rgba(0, 123, 255, 0.3);
    }
  }
}

.goods-list {
  margin-top: 30px;
  width: 100%;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
  
  @media (max-width: 768px) {
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
    gap: 15px;
  }
}
</style>